package edu.ncsu.monopoly.logic;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import edu.ncsu.monopoly.logic.cells.Cell;
import edu.ncsu.monopoly.logic.cells.GoCell;
import edu.ncsu.monopoly.logic.cells.JailCell;
import edu.ncsu.monopoly.logic.cells.PropertyCell;
import edu.ncsu.monopoly.logic.entities.Card;

public class GameBoard {

	private ArrayList<Cell> cells = new ArrayList<Cell>();
    private ArrayList<Card> chanceCards = new ArrayList<Card>();
	//the key of colorGroups is the name of the color group.
	private Hashtable<String, Integer> colorGroups = new Hashtable<String, Integer>();
	private ArrayList<Card> communityChestCards = new ArrayList<Card>();
	final static String JAIL_CELL_NAME = "Jail";
	final static String GO_CELL_NAME = "Go";
	
	
	public GameBoard() {
		Cell go = new GoCell();
		addCell(go);
	}

    public void addCommunityChestCard(Card card) {
    	communityChestCards.add(card);
    }
    
    public void addChanceCard(Card card) {
        chanceCards.add(card);
    }
	
	public void addCell(Cell cell) {
		cells.add(cell);
	}
	
	public void addCell(PropertyCell cell) {
		int propertyNumber = getPropertyNumberForColor(cell.getColorGroup());
		colorGroups.put(cell.getColorGroup(), new Integer(propertyNumber + 1));
        cells.add(cell);
	}

    public Card drawCommunityChestCard() {
        return drawCard(communityChestCards);
    }

    public Card drawChanceCard() {
        return drawCard(chanceCards);
    }

    private Card drawCard(List<Card> cardList){
    	Card card = cardList.get(0);
        cardList.remove(0);
        addChanceCard(card);
        
        return card;
    }
    
	public Cell getCell(int newIndex) {
		return cells.get(newIndex);
	}
	
	public int getCellNumber() {
		return cells.size();
	}
	
	public PropertyCell[] getPropertiesInMonopoly(String color) {
		PropertyCell[] monopolyCells = 
			new PropertyCell[getPropertyNumberForColor(color)];
		int counter = 0;
		for (Cell cell : cells) {
			if(cell instanceof PropertyCell) {
				PropertyCell pc = (PropertyCell)cell;
				if(pc.getColorGroup().equals(color)) {
					monopolyCells[counter] = pc;
					counter++;
				}
			}
		}
		return monopolyCells;
	}
	
	public int getPropertyNumberForColor(String name) {
		if(colorGroups.containsKey(name))
			return colorGroups.get(name).intValue();
		else
			return 0;
	}

	public Cell getJailCell(){
		return queryCell(JailCell.JAIL_CELL_NAME);
	}
	
	public Cell getGoCell(){
		return queryCell(GoCell.GO_CELL_NAME);
	}
	
	public Cell queryCell(String string) {
		for(Cell cell : cells){
			if(cell.getName().equals(string)) {
				return cell;
			}
		}
		return null;
	}
	
	public int queryJailCellIndex(){
		return queryCellIndex(JailCell.JAIL_CELL_NAME);
	}
	
	public int queryCellIndex(String string){
		for(Cell cell : cells){
			if(cell.getName().equals(string)) {
				return cells.indexOf(cell);
			}
		}
		return -1;
	}

    public void removeCards() {
        communityChestCards.clear();
    }
    
    public int getDistanceBetweenCells(String from, String to){
        int toCell = queryCellIndex(to);
        int fromCell = queryCellIndex(from);
        int difference = toCell - fromCell;
        
        if(fromCell > toCell) {
            difference += GameMaster.instance().getGameBoard().getCellNumber();
        }
        
        return difference;
    }
}
